package com.itheima.mobile;

import com.alibaba.dubbo.common.utils.Assert;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.utils.JedisUtils;
import com.itheima.utils.SMSUtils;
import com.itheima.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * @description:
 * @author: dalao
 * @date: 2020/4/11-16:01
 * @Version: 1.0.0
 */
@RequestMapping("/validateCode")
@RestController
public class ValidateCodeController {
    @Autowired
    JedisUtils jedisUtils;
    @Autowired
    DefaultKaptcha defaultKaptcha;
//    发送手机验证码方法
    @RequestMapping("/send4Order")
    public Result send4Order(String telephone){

       try {
//        生成随机验证码
           Integer validateCode = ValidateCodeUtils.generateValidateCode(4);
//        调用发送信息工具类
           SMSUtils.sendSMS(telephone, validateCode);
//        存到redis
           jedisUtils.setex(telephone+RedisMessageConstant.SENDTYPE_ORDER,5*60*100,validateCode.toString());
           return Result.OK(MessageConstant.SEND_VALIDATECODE_SUCCESS);
       }catch (Exception e){
           e.printStackTrace();
           return Result.ERROR(MessageConstant.SEND_VALIDATECODE_FAIL);
       }
    }

//    获取图片验证码
    @RequestMapping("/code/{deviceId}")
    public void createCode(@PathVariable String deviceId, HttpServletResponse response) throws IOException {
        Assert.notNull(deviceId,"机器码不能为空");
        response.setHeader("Cache-Control","no-store,no-cache");
        response.setContentType("image/jpeg");
//        生成文字验证码
        String text = defaultKaptcha.createText();
//        生成图片验证码
        BufferedImage image = defaultKaptcha.createImage(text);
//        生成的验证码写到redis中
        jedisUtils.setex(deviceId,60*5,text);
//        获取输出流
        ServletOutputStream out = response.getOutputStream();
//        写会浏览器
        ImageIO.write(image,"JPEG",out);

    }
//    登录发送手机验证码
    @RequestMapping("/send4Login")
    public Result send4Login(String telephone){

        try {
//        生成随机验证码
            Integer validateCode = ValidateCodeUtils.generateValidateCode(4);
//        调用发送信息工具类
            SMSUtils.sendSMS(telephone, validateCode);
//        存到redis
            jedisUtils.setex(telephone+RedisMessageConstant.SENDTYPE_LOGIN,5*60*100,validateCode.toString());
            return Result.OK(MessageConstant.SEND_VALIDATECODE_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return Result.ERROR(MessageConstant.SEND_VALIDATECODE_FAIL);
        }
    }

}
